package net.huashitong.Indicators.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.core.filter.BaseFilter;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.dataBean.IndexDataDTO;
import net.huashitong.Indicators.dataBean.UserAndDeptDataBean;
import net.huashitong.Indicators.entity.AppMessage;
import net.huashitong.Indicators.filter.AppMessageFilter;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AppMessageDao extends StringDao<AppMessage> {

    public List<UserAndDeptDataBean> queryAllUserAndDept(){
        StringBuffer sql = new StringBuffer("select " +
                "c.dept_sys_id \"deptSysId\"," +
                "a.dept_id \"deptId\"," +
                "a.dept_name \"deptName\"," +
                "b.user_id \"userId\"," +
                "c.username \"userName\"," +
                "b.name \"name\"," +
                "b.mobile \"mobile\"," +
                "b.position \"position\", " +
                "b.birthday \"birthday\"," +
                "b.gender \"gender\"," +
                "b.native_addr \"nativeAddr\"," +
                "b.demo \"demo\"," +
                "b.image \"image\"," +
                "b.state \"state\" " +
                " from t_sys_dept a,t_sys_user b,t_sys_login c " +
                "where a.sys_id = c.dept_sys_id and b.sys_id = c.sys_id ");
        SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(UserAndDeptDataBean.class));
        List<UserAndDeptDataBean> list = (List<UserAndDeptDataBean>) query.list();
        return list;
    }

    public List<AppMessage> queryByUserIdOrderCreateDate(AppMessageFilter filter){
        GenericQuery query = createQuery("obj");
//        if(ObjectUtils.isNotBlank(filter.getSender())){
//            query.append(" or obj.sender =:sender ").setParam("sender", filter.getSender());
//        }
        if(ObjectUtils.isNotBlank(filter.getReceiver())){
            query.append(" and obj.receiver =:receiver ").setParam("receiver", filter.getReceiver());
        }
        if(StringUtils.isNotBlank(filter.getReadStatus())){
            query.append(" and obj.readStatus =:readStatus ").setParam("readStatus", filter.getReadStatus());
        }
        query.getOrders().put("createDate", BaseFilter.DESC);
        return query.listResult();
    }

}
